package net.sx.minemodule.mvp.presenters;

import android.text.TextUtils;

import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.Log;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.mvp.bean.CheckRequestImg;
import net.sx.minemodule.mvp.views.MineOrderAfterSalesReqView;

import java.util.HashMap;

public class MineOrderAfterSalesReqPresenter extends BasePresenter<MineOrderAfterSalesReqView> {
    public MineOrderAfterSalesReqPresenter(MineOrderAfterSalesReqView view) {
        super(view);
    }

    /**
     * 描述:  查询初始化数据
     * author:胡邦茂
     * CreateDate: 2019/7/11 21:02
     */
    public void getAfterSalesByOrderId(int orderId) {
        mView.showLoad("加载中..");
        MineApiUtils.getOrderAfterSale(orderId, res -> {
            mView.hideLoad();
            if (res == null) {
                ToastUtil.showToast("未获取到数据");
                return;
            }
            mView.initResult(res);
        }, th -> {
            mView.hideLoad();
            ToastUtil.showToast(th.getMessage());
        });

    }

    /**
     * 描述:提交本次 售后申请
     * author:胡邦茂
     * CreateDate: 2019/7/11 21:01
     * <p>
     *
     * @param map
     */
    public void submit(HashMap<String, Object> map) {
        mView.showLoad("提交中..");
        MineApiUtils.orderAfterCommit(map, res -> {
            mView.hideLoad();
            mView.submitSuccess();
        }, th -> {
            mView.hideLoad();
            ToastUtil.showToast(th.getMessage());
        });
    }

    /**
     * 描述:  上传图片
     * author:  胡邦茂
     * CreateDate:  2019/7/11 19:13
     */
    public void postImageFile(CheckRequestImg item) {
        Log.e("hbm", "上传图片大小" + item.getFile().getAbsoluteFile() + item.getFile().length());
        item.setStatus(CheckRequestImg.STATUS_LOADING);
        mView.dataStatusNotify(item);

        MineApiUtils.postFile(item, path -> {
            if (TextUtils.isEmpty(path)) {
                item.setStatus(CheckRequestImg.STATUS_ERR);
            } else {
                item.setStatus(CheckRequestImg.STATUS_OK);
                item.setUrl(path);
            }
            mView.dataStatusNotify(item);
        }, throwable -> {
            throwable.printStackTrace();
            Log.e("hbm", "图片走异常");
            ToastUtil.showToast(throwable.getMessage());
            item.setStatus(CheckRequestImg.STATUS_ERR);
            mView.dataStatusNotify(item);
        });


    }
}
